package com.haiqiu.tools.test.controller;

import com.haiqiu.tools.test.entity.SysUser;
import com.haiqiu.tools.excel.utils.ExportCSVUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文档测试
 *
 * @author haiqiu
 * @date 2022/6/28
 */
@RestController
@RequestMapping("/csv")
public class CsvController {

    @RequestMapping(value = "/export")
    public void getSkuList1(HttpServletResponse response) {
        List<Object[]> cellList = new ArrayList<>();
        Object[] obj1 = {1, "小明", 13};
        Object[] obj2 = {2, "小强", 14};
        Object[] obj3 = {3, "小红", 15};
        cellList.add(obj1);
        cellList.add(obj2);
        cellList.add(obj3);

        String[] tableHeaderArr = {"id", "姓名", "年龄"};
        String fileName = "导出文件.csv";
        byte[] bytes = ExportCSVUtil.writeCsvAfterToBytes(tableHeaderArr, cellList);
        ExportCSVUtil.responseSetProperties(fileName, bytes, response);
    }

    @RequestMapping("/getListToCsv")
    public void getListToCsv() {
        List<SysUser> sysUsers = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setActive(true);
            sysUser.setAvatar("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202107%2F23%2F20210723210602_a25c3.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658821183&t=52541efb592f3918b7fa72b9b2582d5f");
            sysUser.setPassword("000000");
            sysUser.setPhone(ExcelController.RandomPhoneNumber.createMobile(0));
            sysUser.setEmail(ExcelController.RandomInfo.getRandomQQEmail());
            sysUser.setNickname(ExcelController.RandomInfo.getRandomGirlName());
            sysUser.setQq(ExcelController.RandomInfo.getRandomPhone());
            sysUser.setWechat(ExcelController.RandomInfo.getRandomPhone());
            sysUser.setSex(ExcelController.RandomInfo.getRandomAge(0, 2));
            sysUser.setUsername(ExcelController.RandomInfo.getRandomGirlName());
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateTime(new Date());
            sysUser.setLoginDate(new Date());
            sysUsers.add(sysUser);
        }
        ExportCSVUtil.writeCsvAfterToFiles(SysUser.class, sysUsers, "导出用户列表文件.csv");
    }
}
